import { post } from "./request";

export async function add(domain, param = {}) {
  const { data } = await post(`api/${domain}/add`, param);
  return data || false;
}

export async function update(domain, mu, data = {}) {
  const { data } = await post(`api/${domain}/update`, { mu, data });
  return data || false;
}

export async function one(domain, mu) {
  const { data } = await post(`api/${domain}/one`, { mu });
  return data || {};
}

export async function list(domain, data = {}) {
  const { data } = await post(`api/${domain}/list`, data);
  return data || [];
}

export async function page(domain, data = {}, pageNum = 1, pageSize = 15) {
  const { page: pageData } = await post(`api/${domain}/page`, {
    pageNum,
    pageSize,
    data
  });
  const { list: listData, current, total } = pageData;
  return listData
    ? { list: listData, total, current }
    : { list: [], total: 0, current };
}

export async function search(uri, data = {}, pageNum = 1, pageSize = 15) {
  const { page: pageData } = await post(uri, { pageNum, pageSize, data });
  const { list: listData, current, total } = pageData;
  return listData
    ? { list: listData, total, current }
    : { list: [], total: 0, current };
}
export async function query(uri, data = {}) {
  const { data } = await post(uri, data);
  return data || {};
}

export async function submit(uri, data = {}) {
  const { data } = await post(uri, data);
  return data || false;
}

export function throttle(fn, data = {}, delay = 500, context = null) {
  clearTimeout(fn.timeoutId);
  fn.timeoutId = setTimeout(function() {
    fn.call(context, data);
  }, delay);
}
